﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using Zwms.Client.CommonUI;

namespace Zwms.Client.ManageWms
{
    public partial class TransferGoodsForm : BaseForm
    {
        private StoGoodsStock _selectedRecord;
        public TransferGoodsForm(StoGoodsStock record)
        {
            InitializeComponent();
            this._selectedRecord = new StoGoodsStock();
            this._selectedRecord.Id = record.Id;
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            this._selectedRecord = client.QueryGoodsStock(this._selectedRecord);
            this.InitUCData();
        }

        private void InitUCData()
        {
            this.tbStockCode.Text = this._selectedRecord.Id;
            this.tbIBCode.Text = this._selectedRecord.IBCode;
            this.tbGoodsCode.Text = this._selectedRecord.GoodsCode;
            this.tbGoodsName.Text = this._selectedRecord.GoodsName;
            this.nudPkgQty.Value = this._selectedRecord.PackageQty;
            this.nudPieceQty.Value = this._selectedRecord.PieceQty;
            this.tbSourceArea.Text = this._selectedRecord.AreaName;
            this.tbSourceArea.Tag = this._selectedRecord.AreaCode;
            this.tbSourceShelf.Text = this._selectedRecord.ShelfName;
            this.tbSourceShelf.Tag = this._selectedRecord.ShelfCode;
            this.tbSourceLocation.Text = this._selectedRecord.LocationName;
            this.tbSourceLocation.Tag = this._selectedRecord.LocationCode;
            this.nudTransferPkgQty.Maximum = this._selectedRecord.PackageQty;
            this.nudTransferPieceQty.Maximum = this._selectedRecord.PieceQty;
        }

        private void tbsTargetLocation_SelectButtonClick(object sender, EventArgs e)
        {
            WhLocation locationRecord = new WhLocation();
            locationRecord.WarehouseCode = MainForm.CurrentUser.WarehouseCode;
            locationRecord.StoreGoodsCode = this._selectedRecord.GoodsCode;
            InfoWms.LocationWnd uc = new InfoWms.LocationWnd(locationRecord);
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择目标库位");
            uc.SetParaQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.tbsTargetLocation.SetTextBoxContent(uc.SelectedRecord.LocationName);
                this.tbsTargetLocation.Tag = uc.SelectedRecord.LocationCode;
                this.tbTargetArea.Text = uc.SelectedRecord.AreaName;
                this.tbTargetArea.Tag = uc.SelectedRecord.AreaCode;
                this.tbTargetShelf.Text = uc.SelectedRecord.ShelfName;
                this.tbTargetShelf.Tag = uc.SelectedRecord.ShelfCode;
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.tbsTargetLocation.GetTextBoxContent()))
            {
                MessageBox.Show("请选择目标库位！");
                return;
            }
            StoGoodsStock targetRecord = new StoGoodsStock();
            BilTsfStockLog TsfRecord = new BilTsfStockLog();
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            targetRecord.Id = client.InitGoodsStockCode();
            targetRecord.LocationCode = this.tbsTargetLocation.Tag.ToString();
            targetRecord.WarehouseName = this.tbsTargetLocation.GetTextBoxContent();
            targetRecord.GoodsCode = this.tbGoodsCode.Text.Trim();
            targetRecord.GoodsName = this._selectedRecord.GoodsName;
            targetRecord.GoodsBarCode = this._selectedRecord.GoodsBarCode;
            targetRecord.SingleGoodsCode = this._selectedRecord.SingleGoodsCode;
            targetRecord.GoodsStatus = this._selectedRecord.GoodsStatus;
            targetRecord.GoodsSpec = this._selectedRecord.GoodsSpec;
            targetRecord.WarehouseCode = this._selectedRecord.WarehouseCode;
            targetRecord.WarehouseName = this._selectedRecord.WarehouseName;
            targetRecord.AreaCode = this.tbTargetArea.Tag.ToString();
            targetRecord.AreaName = this.tbTargetArea.Text;
            targetRecord.ShelfCode = this.tbTargetShelf.Tag.ToString();
            targetRecord.ShelfName = this.tbTargetShelf.Text;
            targetRecord.PackageQty = (Int32)this.nudTransferPkgQty.Value;
            targetRecord.PieceQty = (Int32)this.nudTransferPieceQty.Value;
            targetRecord.GoodsTotalQty = targetRecord.PackageQty * targetRecord.GoodsSpec + targetRecord.PieceQty;

            this._selectedRecord.PackageQty = this._selectedRecord.PackageQty - (Int32)this.nudTransferPkgQty.Value;
            this._selectedRecord.PieceQty = this._selectedRecord.PieceQty - (Int32)this.nudTransferPieceQty.Value;
            this._selectedRecord.GoodsTotalQty = this._selectedRecord.PackageQty * this._selectedRecord.GoodsSpec + this._selectedRecord.PieceQty;
          
            //PagerIndex pIndex = new PagerIndex(1, int.MaxValue);
            //IList<StoGoodsStock> targetList = client.QueryGoodsStockList(targetRecord, ref pIndex);
            //if (targetList == null || targetList.Count == 0 || string.IsNullOrWhiteSpace(targetList[0].Id))
            //{
            //    targetRecord = this._selectedRecord;
            //    targetRecord.Id = client.InitGoodsStockCode();
            //    targetRecord.PackageQty = (Int32)this.nudTransferPkgQty.Value;
            //    targetRecord.PieceQty = (Int32)this.nudTransferPieceQty.Value;
            //    targetRecord.GoodsTotalQty = targetRecord.PackageQty * targetRecord.GoodsSpec + targetRecord.PieceQty;
            //    targetRecord.AreaCode = this.tbTargetArea.Tag.ToString();
            //    targetRecord.AreaName = this.tbTargetArea.Text.Trim();
            //    targetRecord.ShelfCode = this.tbTargetShelf.Tag.ToString();
            //    targetRecord.ShelfName = this.tbTargetShelf.Text.Trim();
            //    targetRecord.LocationCode = this.tbsTargetLocation.Tag.ToString();
            //    targetRecord.LocationName = this.tbsTargetLocation.GetTextBoxContent();
            //}
            //else
            //{
            //    targetRecord = targetList[0];
            //    targetRecord.PackageQty = targetRecord.PackageQty + (Int32)this.nudTransferPkgQty.Value;
            //    targetRecord.PieceQty = targetRecord.PieceQty + (Int32)this.nudTransferPieceQty.Value;
            //    targetRecord.GoodsTotalQty = targetRecord.PackageQty * targetRecord.GoodsSpec + targetRecord.PieceQty;
            //}

            //this._selectedRecord.PackageQty = this._selectedRecord.PackageQty - (Int32)this.nudTransferPkgQty.Value;
            //this._selectedRecord.PieceQty = this._selectedRecord.PieceQty - (Int32)this.nudTransferPieceQty.Value;
            //this._selectedRecord.GoodsTotalQty = this._selectedRecord.PackageQty * this._selectedRecord.GoodsSpec + this._selectedRecord.PieceQty;

            TsfRecord.Id = client.InitTsfBillCode();
            TsfRecord.StockCode = this._selectedRecord.Id;
            TsfRecord.GoodsCode = this._selectedRecord.GoodsCode;
            TsfRecord.GoodsName = this._selectedRecord.GoodsName;
            TsfRecord.PlanPkgGoodsQty = (Int32)this.nudTransferPkgQty.Value;
            TsfRecord.PlanPieceGoodsQty = (Int32)this.nudTransferPieceQty.Value;
            TsfRecord.PkgGoodsQty = TsfRecord.PlanPkgGoodsQty;
            TsfRecord.PieceGoodsQty = TsfRecord.PlanPieceGoodsQty;
            TsfRecord.SourceAreaCode = this._selectedRecord.AreaCode;
            TsfRecord.SourceAreaName = this._selectedRecord.AreaName;
            TsfRecord.SourceShelfCode = this._selectedRecord.ShelfCode;
            TsfRecord.SourceShelfName = this._selectedRecord.ShelfName;
            TsfRecord.SourceLocationCode = this._selectedRecord.LocationCode;
            TsfRecord.SourceLocationName = this._selectedRecord.LocationName;
            TsfRecord.TargetAreaCode = targetRecord.AreaCode;
            TsfRecord.TargetAreaName = targetRecord.AreaName;
            TsfRecord.TargetShelfCode = targetRecord.ShelfCode;
            TsfRecord.TargetShelfName = targetRecord.ShelfName;
            TsfRecord.TargetLocationCode = targetRecord.LocationCode;
            TsfRecord.TargetLocationName = targetRecord.LocationName;
            TsfRecord.IBCode = this._selectedRecord.IBCode;
            TsfRecord.GoodsBarCode = this._selectedRecord.GoodsBarCode;
            TsfRecord.SingleGoodsCode = this._selectedRecord.SingleGoodsCode;
            TsfRecord.OperatorCode = MainForm.CurrentUser.Id;
            TsfRecord.OperatorName = MainForm.CurrentUser.UserName;
            TsfRecord.WarehouseCode = MainForm.CurrentUser.WarehouseCode;
            TsfRecord.WarehouseName = MainForm.CurrentUser.WarehouseName;
            DateTime dtNow = client.GetCurrentSystemTime();
            TsfRecord.TsfDate = Helper.CommonHelper.GetDateString(dtNow);

            try
            {
                if (this._selectedRecord.PackageQty > 0 || this._selectedRecord.PieceQty > 0)
                {
                    client.UpdateGoodsStock(this._selectedRecord);
                }
                else
                {
                    client.DeleteGoodsStock(this._selectedRecord);
                }

                client.AddGoodsStock(targetRecord);
                client.AddTsfStockLog(TsfRecord);
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch
            {
                MessageBox.Show("移库失败！");
            }
        }

        private void checkBoxTemp_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxTemp.CheckState == CheckState.Checked)
            {
                this.tbsTargetLocation.SetEditState(false);
                this.tbTargetArea.Text = string.Empty;
                this.tbTargetArea.Tag = string.Empty;
                this.tbTargetShelf.Text = string.Empty;
                this.tbTargetShelf.Tag = string.Empty;
                this.tbsTargetLocation.Tag = "temp";
                this.tbsTargetLocation.SetTextBoxContent("临时缓存区");
            }
            else if (checkBoxTemp.CheckState == CheckState.Unchecked)
            {
                this.tbsTargetLocation.SetEditState(true);
                this.tbsTargetLocation.Tag = string.Empty;
                this.tbsTargetLocation.SetTextBoxContent(string.Empty);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
    }
}
